package com.zimi.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zimi.mapper.SysOrgExtendMapper;
import com.zimi.model.SysOrgExtend;
import com.zimi.model.SysUser;
import com.zimi.model.SysUserExtend;
import com.zimi.service.SysPostService;
import com.zimi.service.SysUserService;
import com.zimi.util.GsonUtil;
import com.zimi.util.UserUtil;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 系统用户表 apilistByPosts
 *
 * @author lnintr
 * @since 2019-05-23
 */
@RestController
@RequestMapping("sys-user")
public class SysUserApi {
    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysOrgExtendMapper sysOrgExtendMapper;


    @Autowired
    private SysPostService sysPostService;

    @PostMapping("get")
    @ApiOperation("前台获取登录用户")
    public Object getUser(HttpServletRequest request) {
        SysUser user = UserUtil.getUser();

        List<SysOrgExtend> posts = sysPostService.getPostList(user.getId());

        Map<String, Object> objectMap = new HashMap<>();
        objectMap.put("code", 0);
        objectMap.put("sysUser", user);
        objectMap.put("postList", posts);
        return objectMap;
    }


    @PostMapping("listByPosts")
    public Object listByPosts(HttpServletRequest request, String userName, int page, int limit) {
        String[] ma = request.getParameterValues("postList");
        List<String> postIds = ma == null ? null : GsonUtil.json2Objs(ma[0], String.class);

        List<SysUserExtend> list = null;
        long count = 0L;

        PageHelper.startPage(page, limit);
        list = sysUserService.selectUserList(postIds, userName);
        PageInfo<SysUserExtend> pageInfo = new PageInfo<>(list);
        count = pageInfo.getTotal();

        Map map = new HashMap();
        map.put("code", 0);
        map.put("data", list == null ? new ArrayList<>() : list);
        map.put("count", count);
        return map;
    }

    @GetMapping("listKey")
    public Object listAll(String key) {
        List<SysUser> tempList = new SysUser().selectList(new QueryWrapper<SysUser>().lambda().like(SysUser::getUserName, key).or().like(SysUser::getId, key).or().like(SysUser::getLoginName, key));
        List<String> list = new ArrayList<>();

        for (SysUser utemp : tempList) {
            list.add(utemp.getUserName() + " - " + utemp.getId() + " - " + utemp.getLoginName());
        }

        Map map = new HashMap();
        map.put("code", 0);
        map.put("data", list);

        return map;
    }

    @PostMapping("recycleUserList")
    public Object recycleUserList(SysUser user, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<SysUser> list = sysUserService.selectRecycleList(user);
        PageInfo<SysUser> pageInfo = new PageInfo<>(list);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("data", list);
        map.put("count", pageInfo.getTotal());
        return map;
    }

    @PostMapping("recycle")
    public Object del(HttpServletRequest request) {
        String[] ma = request.getParameterValues("userList");

        List<SysUser> sysuserList = GsonUtil.json2Objs(ma[0], SysUser.class);
        Map map = new HashMap();
        map.put("code", sysUserService.recycle(sysuserList));
        return map;
    }

    @PostMapping("searchPostName")
    public Object searchPostName(String[] arr) {
        Map map = new HashMap();
        map.put("code", 0);
        map.put("data", sysUserService.searchUpostNameList(arr));
        return map;
    }

    @PostMapping("update")
    public Object update(HttpServletRequest request, HttpServletResponse response) {
        String[] ma = request.getParameterValues("sysUser");
        SysUserExtend user = GsonUtil.json2Obj(ma[0], SysUserExtend.class);

        sysUserService.update(user);
        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }

    @PostMapping("delete")
    public Object delete(SysUser user) {
        sysUserService.delete(user);

        Map map = new HashMap();
        map.put("code", 0);
        return map;
    }


    @GetMapping("getUserListFromIds")
    @ApiOperation("根据 user ids 获取 user 的基本信息和机构、岗位等信息")
    public Object getUserListFromIds(String userIds) {
        Map map = new HashMap();
        List<SysOrgExtend> userList = new ArrayList<>();
        if (userIds.equals("[]") || userIds == null || userIds.length() == 0) {

        } else {
            userIds = userIds.replace("[", "");
            userIds = userIds.replace("]", "");
            List<String> tempList = Arrays.asList(userIds.split(","));
            List<Integer> userIdList = new ArrayList<>();
            for (String str : tempList) {
                userIdList.add(Integer.parseInt(str));
            }
            userList = sysOrgExtendMapper.selectUserListByIds(userIdList);
        }


        map.put("code", 0);
        map.put("userList", userList);
        map.put("total", userList.size());

        return map;
    }

    @PostMapping("{id}")
    @ApiOperation("根据Id查询信息")
    public Object getUserById(@PathVariable("id") int id) {
        Map map = new HashMap();
        SysUser sysUser = new SysUser();
        SysUser user = sysUser.selectById(id);
        map.put("code", 0);
        map.put("sysUser", user);

        return map;
    }

    @PostMapping("getPostIdList/{id}")
    public Object getPostIdList(@PathVariable("id") int id, int page, int limit) {
        PageHelper.startPage(page, limit);
        List<SysUserExtend> list = sysUserService.getAllUsersByOrgId(id);
        PageInfo<SysUserExtend> pageInfo = new PageInfo<>(list);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("data", list);
        map.put("count", pageInfo.getTotal());
        return map;
    }

    @PostMapping("getPostIdListAll/{id}")
    public Object getPostIdListAll(@PathVariable("id") int id) {
        List<SysUserExtend> list = sysUserService.getAllUsersByOrgId(id);
        Map map = new HashMap();
        map.put("code", 0);
        map.put("data", list);
        return map;
    }

    @PostMapping("listAllCurActivityUsers")
    @ApiOperation("根据选择的筛选条件查询当前活动指定的全部人员")
    public Object listAllCurActivityUsers(int id) {
        Map<String, Object> objectMap = new HashMap<>();
        try {
            objectMap.put("userList", sysOrgExtendMapper.selectUserListByActivity(id));
        } catch (Exception e) {
            objectMap.put("OK", "error");
            objectMap.put("code", 400);
            objectMap.put("message", e.getMessage());
        }
        objectMap.put("OK", "OK");
        objectMap.put("code", 200);
        return objectMap;
    }


}



